Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports ASPNET.StarterKit.BusinessLogicLayer

Public Partial Class Calendar_aspx
    Inherits System.Web.UI.Page

    Public _controlToEdit As String
    Public _isPostBack As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        If (Not Page.IsPostBack) Then

            _controlToEdit = Request.QueryString("controlID")
            Session.Add("controlToEdit", _controlToEdit)
            _isPostBack = Request.QueryString("isPostBack")
            Session.Add("isPostBack", _isPostBack)

            ' Cast first day of the week from web.config file.  Set it to the calendar
            Cal.FirstDayOfWeek = CType(Convert.ToInt32(ConfigurationManager.AppSettings("FirstDayOfWeek")), System.Web.UI.WebControls.FirstDayOfWeek)
            'We must set a selected date to be able to properly fill in the dropdowns
            'For now we use the current date
            Cal.SelectedDate = DateTime.Now


            ' Fills in correct values for the dropdown menus
            FillCalendarChoices()
            SelectCorrectValues()

        Else

            If Not Session("controlToEdit") Is Nothing Then
                _controlToEdit = CStr(Session("controlToEdit"))
            End If

            If Not Session("isPostBack") Is Nothing Then
                _isPostBack = CStr(Session("isPostBack"))
            End If

        End If

    End Sub

    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete

        'OKButton.OnClientClick = "javascript:window.opener.SetControlValue('" & _controlToEdit & "','" & lblDate.Text & "','" & Me.IsPostBack & "');"

    End Sub

    Protected Sub FillCalendarChoices()

        Dim thisdate As DateTime = (DateTime.Now).AddYears(5)

        ' Fills in month values
        For x As Integer = 0 To 11
            ' Loops through 12 months of the year and fills in each month value
            Dim li As ListItem = New ListItem(thisdate.ToString("MMMM"), thisdate.Month.ToString())
            MonthSelect.Items.Add(li)
            thisdate = thisdate.AddMonths(1)
        Next x

        ' Fills in year values and change y value to other years if necessary
        Dim y As Integer = 2000
        Do While y <= thisdate.Year
            YearSelect.Items.Add(y.ToString())
            y = y + 1
        Loop

    End Sub

    Protected Sub SelectCorrectValues()

        'lblDate.Text = Cal.SelectedDate.ToShortDateString()
        'datechosen.Value = lblDate.Text
        MonthSelect.SelectedIndex = MonthSelect.Items.IndexOf(MonthSelect.Items.FindByValue(Cal.SelectedDate.Month.ToString()))
        YearSelect.SelectedIndex = YearSelect.Items.IndexOf(YearSelect.Items.FindByValue(Cal.SelectedDate.Year.ToString()))

    End Sub

    Protected Sub Cal_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cal.SelectionChanged

        Cal.VisibleDate = Cal.SelectedDate
        SelectCorrectValues()

    End Sub

    Protected Sub MonthSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MonthSelect.SelectedIndexChanged

        Cal.VisibleDate = New DateTime(CInt(YearSelect.SelectedItem.Value), CInt(MonthSelect.SelectedItem.Value), 1)
        Cal.SelectedDate = Cal.VisibleDate

        SelectCorrectValues()

    End Sub

    Private Sub DatePicker_DayRender(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DayRenderEventArgs) Handles Cal.DayRender

        Dim hl As New HyperLink()
        hl.Text = CType(e.Cell.Controls(0), LiteralControl).Text
        hl.NavigateUrl = "javascript:window.opener.SetControlValue('" & _controlToEdit & "','" & e.Day.Date.ToShortDateString() & "','" & Me.IsPostBack & "');"
        e.Cell.Controls.Clear()
        e.Cell.Controls.Add(hl)

    End Sub


    Protected Sub YearSelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Cal.VisibleDate = New DateTime(Convert.ToInt32(YearSelect.SelectedItem.Value), Convert.ToInt32(MonthSelect.SelectedItem.Value), 1)
        Cal.SelectedDate = Cal.VisibleDate

        SelectCorrectValues()
    End Sub

End Class
